package me.zhengjie.modules.app.rest;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import me.zhengjie.exception.BadRequestException;
import me.zhengjie.modules.app.domain.GameRounds;
import me.zhengjie.modules.app.domain.dto.GameRoundsDto;
import me.zhengjie.modules.app.service.GameRoundsService;
import me.zhengjie.utils.StringUtils;
import net.dreamlu.mica.core.result.R;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Slf4j
@Api("app/api/v1/game-rounds")
@RestController
@RequestMapping("/app/api/v1/game-rounds")
public class GameRoundsController {
    @Autowired
    private GameRoundsService gameRoundsService;
    /**
     * 用户开局
     * @param dto
     * @return
     */
    @PostMapping("/opening")
    public ResponseEntity opening(@RequestBody GameRoundsDto dto){
        if(StringUtils.isEmpty(dto.getRecordId())){
            throw new BadRequestException("未获取到对局");
        }
        if(StringUtils.isEmpty(dto.getRecordId())){
            throw new BadRequestException("未获取到初始牌局");
        }
        Integer count = gameRoundsService.findByRecordIdCount(dto.getRecordId());
        count = count == null ? 1 : count+1;
        GameRounds gameRounds = new GameRounds();
        gameRounds.setCreateTime(new Date());
        gameRounds.setRecordId(dto.getRecordId());
        gameRounds.setRoundNum(count);
        gameRounds.setInitialCards(dto.getInitialCards());
        int num = gameRoundsService.insertData(gameRounds);
        if(num > 0){
            GameRoundsDto currentGameRoundsDto = gameRoundsService.findOne(gameRounds.getId());
            return ResponseEntity.ok(currentGameRoundsDto);
        }
        return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
    }

    @PostMapping("/gameOver")
    public ResponseEntity gameOver(@RequestBody GameRoundsDto dto){
        if(StringUtils.isEmpty(dto.getId())){
            throw new BadRequestException("未获取到对局详情id");
        }
        if(StringUtils.isEmpty(dto.getCardPlayActionSeq())){
            throw new BadRequestException("未获取到出牌记录");
        }
        if(null == dto.getUserOpen()){
            throw new BadRequestException("未获取到谁先手");
        }
        if(null == dto.getScoreP1() || null == dto.getScoreP2()){
            throw new BadRequestException("未获取到得分");
        }
        if(null == dto.getWinnerId()){
            throw new BadRequestException("未获取到赢家");
        }
        if(null == dto.getWinnerType()){
            throw new BadRequestException("未获取到赢家类型");
        }
        if(null == dto.getDuration()){
            throw new BadRequestException("未获取到本局时长");
        }
        GameRounds gameRounds = new GameRounds();
        BeanUtils.copyProperties(dto,gameRounds);
        gameRounds.setId(Long.valueOf(dto.getId()));
        int num = gameRoundsService.updateData(gameRounds);
        if(num > 0){
            return ResponseEntity.ok("记录成功");
        }
        return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
    }

    @GetMapping("/statisticsRoundsInfo")
    public R statisticsRoundsInfo(@RequestParam("userId")Long userId){
        return R.success(gameRoundsService.totalRoundsInfo(userId));
    }
    @GetMapping("/list")
    public R list(GameRoundsDto dto){
        QueryWrapper<GameRounds> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("record_id", dto.getRecordId() );
        List<GameRounds> result = gameRoundsService.list(queryWrapper);
        return R.success(result);
    }
}
